/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.web.action.general;

import foo.bar.blog.core.domain.Blog;
import foo.bar.blog.core.domain.Entry;
import foo.bar.blog.core.services.BlogService;
import foo.bar.blog.web.action.ExtendedPaginatedList;
import foo.bar.blog.web.action.PaginatedBaseActionBean;
import net.sourceforge.stripes.action.After;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import org.perf4j.aop.Profiled;

import java.util.List;

/**
 * Actions related to the main portal page of the system.
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
public class MainActionBean extends PaginatedBaseActionBean {

    public static final String VIEW = "/main_recent_entries.jsp";
    public static final String VIEW_WEBLOG_DIRECTORY = "/main_weblog_directory.jsp";

    public static final String VIEW_SEARCH_ENTRIES_RESULT = "/main_search_entries_result.jsp";



    private BlogService blogService;

    private List<Entry> latestEntries;
    private List<Blog> latestBlogs;


    private String weblogDirectoryCharacter;
    private ExtendedPaginatedList weblogDirectories;
    private ExtendedPaginatedList searchEntriesResult;


    //@Validate(trim = true, required = true, on="searchEntries")
    private String searchEntryCriteria;

    //@Validate(trim = true, required = true, on="searchDirectories")
    private String searchDirectoryCriteria;


    /**
     * Display the main portal page (latest blog entries listing).
     * Input - none
     * Output - latestEntries, latestBlogs
     * @return
     */
    @DefaultHandler
    @Profiled(tag="MainActionBean_view", logFailuresSeparately = true)
    public Resolution view() {
        latestEntries = blogService.getLatestPublishedEntries();
        latestBlogs = blogService.getLatestBlogs();

        return new ForwardResolution(VIEW);
    }


    /**
     * Display the weblog directories listing page
     * Input - none
     * Output - latestBlogs, weblogDirectories
     * @return
     */
    @Profiled(tag="MainActionBean_viewWeblogDirectories", logFailuresSeparately = true)
    public Resolution viewWeblogDirectories() {
        latestBlogs = blogService.getLatestBlogs();
        weblogDirectories = blogService.getWeblogDirectories(getPaginatedList("weblogDirectories"), weblogDirectoryCharacter);
        return new ForwardResolution(VIEW_WEBLOG_DIRECTORY);
    }
    @After(stages = {LifecycleStage.BindingAndValidation}, on="viewWeblogDirectories")
    public void after_viewWeblogDirectories() {
        if (weblogDirectoryCharacter == null) { // if it's not given, see if it's being saved.
            weblogDirectoryCharacter = (String) getSessionAttribute("weblogDirectoryCharacter");
            if (weblogDirectoryCharacter == null) { // still not there? use 'A'
                weblogDirectoryCharacter = "A";
                clearPaginatedList("weblogDirectoryCharacter");
            }
        }
        else { // if it's being given, save it for latter use
            setSessionAttribute("weblogDirectoryCharacter", weblogDirectoryCharacter);
            clearPaginatedList("weblogDirectoryCharacter");
        }
    }


    /**
     * Search blog entries and display results.
     * Input - searchEntryCriteria
     * Output - searchEntriesResult, latestBlogs
     * @return
     */
    @Profiled(tag="MainActionBean_searchEntries", logFailuresSeparately = true)
    public Resolution searchEntries() {
        ExtendedPaginatedList paginatedList = getPaginatedList("searchEntry");
        latestBlogs = blogService.getLatestBlogs();
        searchEntriesResult = blogService.searchBlogs(searchEntryCriteria, paginatedList);
        return new ForwardResolution(VIEW_SEARCH_ENTRIES_RESULT);
    }


    /**
     * Search weblog directories and display results.
     * Input - searchDirectoryCriteria
     * Output - latestBlogs, weblogDirectories
     * @return
     */
    @Profiled(tag="MainActionBean_searchDirectories", logFailuresSeparately = true)
    public Resolution searchDirectories() {
        ExtendedPaginatedList paginatedList = getPaginatedList("searchDirectory");
        latestBlogs = blogService.getLatestBlogs();
        weblogDirectories = blogService.searchDirectories(searchDirectoryCriteria, paginatedList);
        return new ForwardResolution(VIEW_WEBLOG_DIRECTORY);
    }




    public void setSearchDirectoryCriteria(String searchDirectoryCriteria) {
        this.searchDirectoryCriteria = searchDirectoryCriteria;
    }

    public void setSearchEntryCriteria(String searchEntryCriteria) {
        this.searchEntryCriteria = searchEntryCriteria;
    }

    public ExtendedPaginatedList getSearchEntriesResult() {
        return searchEntriesResult;
    }

    public String getWeblogDirectoryCharacter() {
        return weblogDirectoryCharacter;
    }

    public void setWeblogDirectoryCharacter(String weblogDirectoryCharacter) {
        this.weblogDirectoryCharacter = weblogDirectoryCharacter;
    }

    public ExtendedPaginatedList getWeblogDirectories() {
        return weblogDirectories;
    }

    public void setWeblogDirectories(ExtendedPaginatedList weblogDirectories) {
        this.weblogDirectories = weblogDirectories;
    }

    public List<Entry> getLatestEntries() {
        return latestEntries;
    }

    public void setLatestEntries(List<Entry> latestEntries) {
        this.latestEntries = latestEntries;
    }

    public List<Blog> getLatestBlogs() {
        return latestBlogs;
    }

    public void setLatestBlogs(List<Blog> latestBlogs) {
        this.latestBlogs = latestBlogs;
    }

    @SpringBean("blogService")
    public void setBlogService(BlogService blogService) {
        this.blogService = blogService;
    }



}
